package com.mconnect.msurvey.controller;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import com.mconnect.msurvey.model.TechnicalQuestions;
import com.mconnect.msurvey.util.HibernateUtil;

public class TechnicalQuestionsManager {
	
	protected List<String> getNonEmptyTechnicalFields(TechnicalQuestions technical){
		List<String> technicalList = new ArrayList<String>();
		if(!technical.getDevelopingPrograms().equals("none")){
			technicalList.add("developingPrograms='" + technical.getDevelopingPrograms() + "'");
		}if(!technical.getDirection().equals("all")){
			technicalList.add("direction='" + technical.getDirection() + "'");
		}if(!technical.getExperience().equals("all")){
			technicalList.add("experience='" + technical.getExperience() + "'");
		}
		
		return technicalList;
	}
	
	protected String getSQLQueryTechnical(List<String> nonEmptyTechnicalFields){
		int number = nonEmptyTechnicalFields.size();
		String sqlQuery = "";
		if(number == 1){
			sqlQuery = "select id_survey from technical_questions where " + nonEmptyTechnicalFields.get(0);
		}else if(number == 2){
			sqlQuery = "select id_survey from technical_questions where " + nonEmptyTechnicalFields.get(0) + " and " + nonEmptyTechnicalFields.get(1);
		}else if(number == 3){
			sqlQuery = "select id_survey from technical_questions where " + nonEmptyTechnicalFields.get(0) + " and " + nonEmptyTechnicalFields.get(1) + " and " + nonEmptyTechnicalFields.get(2);
		}
		
		return sqlQuery;
	}
	
	@SuppressWarnings("unchecked")
	public List<TechnicalQuestions> getQuestionsByIdSurvey(int id_survey){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
        
        List<TechnicalQuestions> questionsList = new ArrayList<TechnicalQuestions>();
        questionsList = session.createQuery("select t from TechnicalQuestions t LEFT JOIN FETCH t.id_language where id_survey=" + id_survey).list();
        
        session.getTransaction().commit();
		return questionsList;
	}
	
	
	
	

}
